'Populates the Trustee Disclosure form with the OutsideEntity Name - Answer Filler
Option Strict Off
imports System
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports PHSRAG.Insight21.FormsManagement
imports PHSRAG.Insight21.COI
imports System.Collections

module Script
    sub Main()
        dim outsideEntityAnswer as PHSRAG.Insight21.FormsManagement.Answer
		dim oeAnswerFound as boolean = false
        
        if (questAnswer.Id > 0) then
			dim answers as ArrayList = questAnswer.GetAnswers("Employer")
			if (answers.Count > 0) then
				outsideEntityAnswer = answers(0)
				oeAnswerFound = true
			end if
		end if

		if (not oeAnswerFound) then
			outsideEntityAnswer = new PHSRAG.Insight21.FormsManagement.Answer(cachePolicy)
			outsideEntityAnswer.QuestionnaireAnswerId = questAnswer.Id
			outsideEntityAnswer.Name = "Employer"
			questAnswer.Answers.Add(outsideEntityAnswer)
		end if

		outsideEntityAnswer.Value = Convert.ToString(coiParams.OutsideEntityName)
		
		questAnswer.Name = "Trustee Disclosure"
		currentCOI.QuestionnaireAnswers.Add(questAnswer)	
		currentCOI.Save(connectionString) 'takes care of the mapping record
		
		currentCOI.LoadQuestionnaireAnswers()
		dim questionnaireAnswers as ArrayList = currentCOI.QuestionnaireAnswers
        dim initialQuestionnaireAnswer as QuestionnaireAnswer

		For Each questionnaireAnswer as QuestionnaireAnswer in questionnaireAnswers
			If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.InitialQuestionnaire) Then
				
					initialQuestionnaireAnswer = questionnaireAnswer

					dim parentAnswerList as ArrayList = questionnaireAnswer.GetAnswers("OutsideActivity")

					if parentAnswerList.Count > 0 Then						
						dim answers as ArrayList = questionnaireAnswer.GetAnswers("ListOutsideActivities")
						if not answers is Nothing Then
							For Each answer as Answer in answers								
								answer.MarkForDelete = true
							Next
						End If
						' Set to N because OA could be set to Y, but the child answers were N, so it didn't rise to the level
						dim parentAnswer as Answer = parentAnswerList(0)
						parentAnswer.Value = "N"
					End If
					parentAnswerList = nothing
					parentAnswerList = questionnaireAnswer.GetAnswers("YourEquityInterests")

					if parentAnswerList.Count > 0 Then						
						dim answers as ArrayList = questionnaireAnswer.GetAnswers("ListEquityInterests")
						if not answers is Nothing Then
							For Each answer as Answer in answers								
								answer.MarkForDelete = true							
								
							Next
						End If
					End If
					parentAnswerList = nothing
					parentAnswerList  = questionnaireAnswer.GetAnswers("YourFamily")

					if parentAnswerList.Count > 0 Then						
						dim answers as ArrayList = questionnaireAnswer.GetAnswers("ListFamilyAssociations")
						if not answers is Nothing Then
							For Each answer as Answer in answers
								answer.MarkForDelete = true								
								
							Next
						End If
					End If
					initialQuestionnaireAnswer.Save(connectionString)
					exit for
			End If
		Next

		For Each questionnaireAnswer as QuestionnaireAnswer in questionnaireAnswers

			If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.OutsideActivity) Then
				
				dim parentAnswerList as ArrayList = initialQuestionnaireAnswer.GetAnswers("OutsideActivity")
				if parentAnswerList.Count > 0 Then
					dim parentAnswer as Answer = parentAnswerList(0)
					' Since there is an OA questionnaire, set OA to Y
					parentAnswer.Value = "Y"
					dim answer as new Answer(cachePolicy)
					answer.Name = "ListOutsideActivities"
					answer.Value = string.Format("{0}|{1}", questionnaireAnswer.ExternalId.ToString(), questionnaireAnswer.Name.ToString())
					answer.QuestionnaireAnswerId = questionnaireAnswer.Id 
					answer.ParentAnswerId = parentAnswer.Id					
					initialQuestionnaireAnswer.Answers.Add(answer)
					
				End If

			Else If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.EquityInterest) Then

				dim parentAnswerList as ArrayList = initialQuestionnaireAnswer.GetAnswers("YourEquityInterests")
				if parentAnswerList.Count > 0 Then
					dim parentAnswer as Answer = parentAnswerList(0)
					dim answer as new Answer(cachePolicy)
					answer.Name = "ListEquityInterests"
					answer.Value = string.Format("{0}|{1}", questionnaireAnswer.ExternalId.ToString(), questionnaireAnswer.Name.ToString())
					answer.QuestionnaireAnswerId = questionnaireAnswer.Id 
					answer.ParentAnswerId = parentAnswer.Id					
					initialQuestionnaireAnswer.Answers.Add(answer)					

				End If
			Else If (questionnaireAnswer.QuestionnaireCategory = PHSRAG.Insight21.FormsManagement.Constants.COICategory.FamilyAssociation) Then

				dim parentAnswerList as ArrayList = initialQuestionnaireAnswer.GetAnswers("YourFamily")
				if parentAnswerList.Count > 0 Then
					dim parentAnswer as Answer = parentAnswerList(0)
					dim answer as new Answer(cachePolicy)
					answer.Name = "ListFamilyAssociations"
					answer.Value = string.Format("{0}|{1}", questionnaireAnswer.ExternalId.ToString(), questionnaireAnswer.Name.ToString())
					answer.QuestionnaireAnswerId = questionnaireAnswer.Id 
					answer.ParentAnswerId = parentAnswer.Id
					initialQuestionnaireAnswer.Answers.Add(answer)
					
				End If
			End If
		Next
		initialQuestionnaireAnswer.Save(connectionString)
    end sub
end module